1 /* 2 Copyright: Marcelo S. N. Mancini (Hipreme|MrcSnm), 2018 - 2021 3 License: [https://creativecommons.org/licenses/by/4.0/|CC BY-4.0 License]. 4 Authors: Marcelo S. N. Mancini 5 6 Copyright Marcelo S. N. Mancini 2018 - 2021. 7 Distributed under the CC BY-4.0 License. 8 (See accompanying file LICENSE.txt or copy at 9 https://creativecommons.org/licenses/by/4.0/ 10 */ 11 12 module hip.graphics.g2d.tilemap; 13 public import hip.graphics.g2d.spritebatch; 14 public import hip.assets.texture; 15 public import hip.api.data.tilemap; 16 17 void render(HipTileLayer layer, IHipTilemap map, HipSpriteBatch batch, bool shouldRenderBatch = false) 18 { 19 if(layer.width == 0 || layer.height == 0) 20 return; 21 uint w = layer.width, h = layer.height; 22 23 uint th = cast(uint)(map.tileHeight*map.scaleY), 24 tw = cast(uint)(map.tileWidth*map.scaleX); 25 26 ushort lastId; 27 IHipTextureRegion lastTexture; 28 29 30 31 for(int i = 0, y = layer.y; i < h; i++, y+= th) 32 for(int j =0, x = layer.x; j < w; j++, x+= tw) 33 { 34 ushort targetTile = layer.tiles[i*w+j]; 35 if(targetTile == 0) 36 continue; 37 if(lastId != targetTile) 38 { 39 /** 40 * Probably worth caching as it is: 41 * - one pointer dereference (map->) 42 * - one function call (getTextureRegionForID) 43 * - one function call (getTilesetForID) 44 * - one pointer derefenrece (tileset->) 45 * - one function call (getTextureRegion) 46 */ 47 lastId = targetTile; 48 lastTexture = map.getTextureRegionForID(targetTile); 49 } 50 batch.draw(lastTexture, map.x + x, map.y + y, 0, map.color, map.scaleX, map.scaleY, map.rotation); 51 } 52 53 if(shouldRenderBatch) 54 batch.draw(); 55 } 56 57 void render(IHipTilemap map, HipSpriteBatch batch, bool shouldRenderBatch) 58 { 59 foreach(l; map.layers) 60 { 61 render(l, map, batch, false); 62 } 63 if(shouldRenderBatch) 64 batch.draw(); 65 }